package com.example.myapplication.hospital;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LeavecommentBean;
import com.example.myapplication.ShuJu.MyconsultationtalBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class DoctorcaseActivity extends AppCompatActivity {

    private MyconsultationtalBean.RowsBean rowsBean;
    private ImageView img;
    private ImageView avatar;
    private TextView name;
    private TextView jobName;
    private TextView question;
    private EditText inquiry;
    private LinearLayout li_submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctorcase);
        initview();
        //返回
        but_bloak();
        //填充信息
        fillinformation();
    }

    private void fillinformation() {
        MyconsultationtalBean.RowsBean.DoctorBean doctor = rowsBean.getDoctor();
        Glide.with(this).load(TokenBean.IP+doctor.getAvatar()).into(avatar);
        name.setText(doctor.getName());
        jobName.setText(doctor.getJobName());
        question.setText(rowsBean.getQuestion());
        //提问问题
        //问诊记录 ID
        askquestions(rowsBean.getId());
    }

    private void askquestions(Integer ID) {
        li_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content  = inquiry.getText().toString();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("inquiryId",ID);
                    jsonObject.put("content",content);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String JSON = jsonObject.toString();
                followupchat(JSON);
            }
        });
    }

    private void followupchat(String json) {
        OKHttpTool.doNetRequest("prod-api/api/pet-hospital/inquiry-message",
                OKHttpTool.POST,
                LeavecommentBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                LeavecommentBean leavecommentBean = (LeavecommentBean)obj;
                                if(leavecommentBean.getCode()==200){
                                    Toast.makeText(DoctorcaseActivity.this,"追问成功",Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                },json);
    }

    private void but_bloak() {
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DoctorcaseActivity.this.finish();
            }
        });
    }

    private void initview() {
        Intent intent = getIntent();
        String DOCTORCASEACTIVITY = intent.getStringExtra(Commom.DOCTORCASEACTIVITY);
        Gson gson = new Gson();
        rowsBean = gson.fromJson(DOCTORCASEACTIVITY, MyconsultationtalBean.RowsBean.class);
        img = findViewById(R.id.img);
        avatar = findViewById(R.id.avatar);
        name = findViewById(R.id.name);
        jobName = findViewById(R.id.jobName);
        question = findViewById(R.id.question);
        inquiry = findViewById(R.id.inquiry);
        li_submit = findViewById(R.id.li_submit);


    }
}